<?php

use Biz\model\brandActivity\BrandActivityBasic;
use Biz\model\NewProduct\NewProductWinnerListModel;
use Biz\service\brand\BrandActivity;

/**
 * Description: 分享发布动态
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/28
 * Time: 11:01
 */
class SharedNoteAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $uid = $this->uid;
        $nid = $this->getRequest()->getPost('nid');
        $winner_id = $this->getRequest()->getPost('winner_id');

        if (!$nid || !$uid || !$winner_id) {
            throw new \Exception('非法传参！');
        }

        $info = NewProductWinnerListModel::first(['nid' => $nid, 'uid' => $uid, 'id' => $winner_id],
            ['uid', 'type', 'nid', 'shared_status', 'is_share']);

        if (!$info) {
            throw new \Exception('您未中奖！');
        }
        //判断是否分享
        if ((int)$info['shared_status'] === NewProductWinnerListModel::SHARED_STATUS) {
            return $this->setSuccess(['msg' => '已分享']);
        }

        $up_data['shared_status'] = NewProductWinnerListModel::SHARED_STATUS;
        $up_data['is_share'] = NewProductWinnerListModel::SHARE;
        $up_data['edittime'] = date("Y-m-d H:i:s");
        $up_where['uid'] = $uid;
        $up_where['nid'] = $nid;
        $up_where['id'] = $winner_id;
        NewProductWinnerListModel::update($up_data, $up_where);
        $log['sql_log'] = NewProductWinnerListModel::log();
        $log['info'] = $info;
        $log['nid'] = $nid;
        $log['updata'] = $up_data;
        $log['where'] = $up_where;
        \Doraemon\tools\Log::debug('发布动态日志', $log);
        $this->_sendNote($info);
        return $this->setSuccess(['msg' => '已发布动态']);

    }

    /**
     * 发布动态
     * @param $info
     * @return bool
     * @throws Exception
     */
    private function _sendNote($info)
    {
        BrandActivity::createDynamic($info['uid'], $info['nid'], BrandActivityBasic::ACTIVITY_TYPE_NEW_PRODUCT);
        return true;
    }
}